<?php

	function get_filelist($dir){ 
		$filelist = array(); 
		$dh = opendir($dir); 
		if ($dh === NULL) return $filelist; 
		else $filelist[] = $dir; 
		while (($file = readdir($dh)) !== false) { 
			if ($file == '.' || $file == '..') continue; 
			if (strpos($file, 'Thumbs.db')) continue; 
			$filelist[] = $dir.'/'.$file; 
			$filename = $dir.'/'.$file; 
			if (is_dir($filename)) { 
				$tmp = get_filelist($filename); 
				$n = sizeof($tmp); 
				for ($i = 0; $i < $n; $i++) $filelist[] = $tmp[$i]; 
			} 
		} 
		closedir($dh);
		return $filelist; 
	}
	
	function copy_client(){
		global $setupdata;
		$files = get_filelist("client");
		foreach($files as $value){
			if (is_file($value)){
				$p = explode("/", $value);
				$x = $setupdata['savepage'];
				for ($i = 0; $i <= count($p)-1; $i++){
					$x .= "/".$p[$i];
					if ($i < count($p)-1){
						if (!file_exists($x)){
							mkdir($x);
						}
					} else {
						if (!file_exists($x) or (filesize($x) == 0)){
							$w = fopen($x, "wb");
							$r = fopen($value, "rb");
							while (!feof($r)){
								$s = fread($r, 1024);
								fwrite($w, $s, strlen($s));
							}
							fclose($w);
							fclose($r);
						}
					}
				}
			} elseif (is_dir($value)){
				$p = explode("/", $value);
				$x = $setupdata['savepage'];
				for ($i = 0; $i <= count($p)-1; $i++){
					$x .= "/".$p[$i];
					if (!file_exists($x)){
						mkdir($x);
					}
				}
			}
		}
	}
	
	function copy_binary(){
		global $setupdata;
		
	}
	
	function copy_html(){
		global $setupdata;
		preg_match_all("'<loc>.*?</loc>'si", $buffer, $content, PREG_SET_ORDER);
		foreach ($content as $val) {
			$val[0] = strip_tags($val[0]);
		}
	}
	
	function smarty_outputfilter_savepage($tpl_source, &$smarty)
	{
		global $setupdata;
		
		$tpl_source = str_replace('<base href="'.$setupdata['base_href'].'/" >', "", $tpl_source);
		
		$file_target = $setupdata['savepage']."/xxx.html";
		$w = fopen($file_target, 'wb');
		fwrite($w, $tpl_source, strlen($tpl_source));
		fclose($w);
		
		copy_client();
		copy_binary();
		
/*
		$self_site = str_replace(":", "\:", $setupdata['base_href']);
		preg_match_all("'".$self_site."/get.php\?.\.[0-9]+([0-9])'si", $tpl_source, $tmp);
		if (isset($tmp[0]) && is_array($tmp[0])){
			foreach($tmp[0] as $key=>$value){
				if (strpos($value, "get.php?i") !== false){
					$x = explode(".", $value);
					$ids[$x[intval(count($x)-1)]] = $value;
				}
			}
		}
*/

/*		
		preg_match_all("/(client\/)(.*?)(\.)(jpg|gif|css)/si", $tpl_source, $tmp);
		if (isset($tmp[1]) && is_array($tmp[1])){
			foreach($tmp[1] as $key=>$value) {
				if (file_exists($tmp[0][$key])){
					print_r($tmp[0][$key]."<br>");
					$p = explode("/", $tmp[0][$key]);
					$x = $setupdata['savepage'];
					for ($i = 0; $i <= count($p)-1; $i++){
						$x .= "/".$p[$i];
						if ($i < count($p)-1){
							if (!file_exists($x)){
								mkdir($x);
							}
						} else {
							if (!file_exists($x) or (filesize($x) == 0)){
								$w = fopen($x, "wb");
								$r = fopen($tmp[0][$key], "rb");
								while (!feof($r)){
									$s = fread($r, 1024);
									fwrite($w, $s, strlen($s));
								}
								fclose($w);
								fclose($r);
							}
						}
					}
				}
			}
		}
*/

		//print_r($img_array);
	
		return $tpl_source;
	}
?>
